package com.spring.sperapc.web.service.impl;

import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.User;
import com.spring.sperapc.web.dao.FamilyMapper;
import com.spring.sperapc.web.dao.FamilyMemberMapper;
import com.spring.sperapc.web.model.Family;
import com.spring.sperapc.web.model.FamilyMember;
import com.spring.sperapc.web.service.UserFamilyService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by wojunyu on 2015/8/12.
 */
@Service
public class UserFamilyServiceImpl implements UserFamilyService{
    @Resource
    private FamilyMapper familyMapper;

    @Resource
    private FamilyMemberMapper familyMemberMapper;

    @Override
    public int deleteByPrimaryKey(@Param(value = "id") String id) {
        return familyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 用户新增家庭，并将自己添加到家庭成员中去
     * @param record
     * @param u
     * @return
     */
    @Override
    public int insertSelective(Family record,User u) {
        int ret = 0;
        int f = familyMapper.insertSelective(record);
        FamilyMember fmModel = new FamilyMember();
        fmModel.setcFamilyId(ApplicationUtils.randomUUID());
        fmModel.setcFamilyId(record.getId());
        fmModel.setcRoleId("1");
        int fm = familyMemberMapper.insertSelective(fmModel);
        if(f==1&&fm==1){
            ret = 1;
        }
        return ret;
    }

    /**
     * 按条件查询家庭信息
     * @param m
     * @return
     */
    @Override
    public Family selectByPrimaryKey(Map m) {
        return familyMapper.selectByPrimaryKey(m);
    }

    /**
     * 修改家庭信息
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(Family record) {
        return familyMapper.updateByPrimaryKeySelective(record);
    }
}
